﻿using Newtonsoft.Json;

namespace Company.Core.Helpers
{
    public class JsonHelper
    {
        public static string Serialize(object obj,bool indented = false)
        {
            Formatting formatting = indented ? Formatting.Indented : Formatting.None;
            return JsonConvert.SerializeObject(obj, formatting);
        }
        public static void Save(object obj,string fileName,bool indented = false)
        {
            string content = Serialize(obj, indented);
            File.WriteAllText(fileName, content);
        }

        public static T Deserialize<T>(string content)
        {
            return JsonConvert.DeserializeObject<T>(content);
        }
        public static T Load<T>(string fileName) where T : class
        {
            var type = typeof(T);
            if (!File.Exists(fileName))
            {
                return default(T);
            }

            string content = File.ReadAllText(fileName);
            return Deserialize<T>(content);
        }


    }
}
